package com.ipay.controller;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.ipay.utils.HttpClientUtils;

/**
 * 商户管理
 * 
 * @author yangk
 * @email sunlightcs@gmail.com
 * @date 2016年11月24日 下午11:05:27
 */
@Controller
public class MerchantController {

	public final static String  Appid = "wxf7fca97aabff09c9";
	//授权后重定向的回调链接地址  参考：https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
	public final static String RedirectUri = "www.iknowpuls.cn/pay/payment.html";

	@RequestMapping("merchant/{url}.html")
	public String page(@PathVariable("url") String url) {
		return "merchant/" + url + ".html";
	}

	@RequestMapping(value = "merchant/userAgent/{merchantNo}", method = RequestMethod.GET)
	public ModelAndView getUserAgent(@PathVariable String merchantNo, HttpServletRequest request,
			final RedirectAttributes redirectAttributes) {
		String ua = request.getHeader("User-Agent");
		/*String no = request.getParameter("merchantNo");*/
		// ua.indexOf("MicroMessenger");//微信
		// ua.indexOf("AlipayClient");//支付宝
		System.out.println(ua);
		System.out.println(merchantNo);
		redirectAttributes.addAttribute("merchantId", merchantNo);
		redirectAttributes.addAttribute("User-Agent", "MicroMessenger");
		String req_url="https://open.weixin.qq.com/connect/oauth2/authorize?"
				+ "appid="+ Appid
				+ "&redirect_uri="+URLEncoder.encode(RedirectUri)
				+ "&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
		try {
			String response = HttpClientUtils.getResponseText(req_url);
			System.out.println(response);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return new ModelAndView("redirect:/pay/payment.html");
	}

}
